package com.xy.smartsms.data;

import android.content.Context;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import static java.util.concurrent.Executors.newFixedThreadPool;

/**
 * Created by admin on 2017/2/16.
 */

public abstract class BaseSmsPlugin implements ISmsPlugin {
    protected Context mContext = null;
    protected Map<String, String> mParams = new HashMap<>();
    private ExecutorService mCardPool = null;
    private ExecutorService mPublicInfoPool = null;

    public BaseSmsPlugin(Context context) {
        mContext = context;
    }

    protected void cardCallback(ISmsPluginCallback callback, int status, int type, JSONObject json) {
        if (callback != null) {
            callback.onCard(status, type, json);
        }
    }

    protected void publicInfoCallback(ISmsPluginCallback callback, int status, PublicInfoData data) {
        if (callback != null) {
            callback.onPublicInfo(status, data);
        }
    }

    protected boolean isNetworkAllowed() {
        if (mParams == null || !mParams.containsKey("SUPPORT_NETWORK_TYPE")) {
            return false;
        }

        int type = Integer.valueOf(mParams.get("SUPPORT_NETWORK_TYPE"));
        return type != 0 ? true : false;
    }


    protected synchronized ExecutorService getCardPool() {
        if (mCardPool == null || mCardPool.isShutdown()) {
            // NOTE: only 1 thread is allowed here. due to Callable is singleton
            mCardPool = newFixedThreadPool(1);
        }

        return mCardPool;
    }

    protected synchronized ExecutorService getPublicInfoPool() {
        if (mPublicInfoPool == null || mPublicInfoPool.isShutdown()) {
            mPublicInfoPool = newFixedThreadPool(1);
        }

        return mPublicInfoPool;
    }

    @Override
    public void init(Object doAction, Map<String, String> extend) {
        mParams.putAll(extend);
    }

    @Override
    public void setParam(Map<String, String> extend) {
        mParams.putAll(extend);
    }

    @Override
    public String getParam(String key) {
        if (mParams == null || mParams.size() == 0) {
            return null;
        }

        return mParams.get(key);
    }

    @Override
    public void parseCardSync(int type, SmsItem item) {

    }

    @Override
    public void backgroundParse(String phoneNum, int limit) {

    }

    @Override
    public void loadCache(String phone) {
    }

    @Override
    public void clearCache(String phone) {
    }
}
